﻿using Learun.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-21 15:32
    /// 描 述：关联类型表
    /// </summary>
    public class ec_CircuitBreakerBLL
    {
        ec_enginedata_relBLL ec_Enginedata_RelBLL;
        ec_CircuitBreakerService ec_CircuitBreakerService;
        public ec_CircuitBreakerBLL()
        {
            ec_CircuitBreakerService = new ec_CircuitBreakerService();
            ec_Enginedata_RelBLL = new ec_enginedata_relBLL();
        }
        #region 获取数据

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ProjID"></param>
        /// <param name="RelTypeId"></param>
        /// <param name="EngineerDataID">分电箱的ID</param>
        /// <param name="OnlySelf"></param>
        /// <returns></returns>
        public IEnumerable<ec_CircuitBreakerEntity> GetBreakersUnderPDB(string ProjID, string EngineerDataID, bool OnlySelf = false)
        {
            try
            {
                var engineBll = new ec_enginedataService();
                var ProjectEntity = new ec_projectEntity();
                ProjectEntity = new ec_projectService().GetEntity(ProjID);
                if (ProjectEntity == null)
                {
                    return null;
                }
                ///查到所有开关的id
                //var relDataCol = ec_Enginedata_RelBLL.GetList("{ProjectId:\"" + ProjID + "\",RelType:\"" + (int)enum_RelType.分电箱_开关 + "\",RelEngineDataID:\"" + EngineerDataID + "\"}");
                //var BreakerIDs = relDataCol.Select(x => x.RelEngineData2ID).Distinct();
                var resAll = ec_CircuitBreakerService.GetList("{ProjectId:\"" + ProjID + "\"}");
                var res = resAll.Where(x => x.PDBId == EngineerDataID).OrderBy(x => x.OrderId).ToList();
                //所有的开关-电缆关系
                //var RelBreaker_CableCol = ec_Enginedata_RelBLL.GetList("{ProjectId:\"" + ProjID + "\",RelType:\"" + (int)enum_RelType.开关_电缆 + "\"}");
                if (!OnlySelf && res != null)
                {

                }

                foreach (var CB in res)
                {
                    //CB.CableIDs = ec_Enginedata_RelBLL.GetList("{ProjectId:\"" + ProjID + "\",RelEngineData1ID:\"" + CB.EngineerDataID + "\"}").
                    //    Where(X => !string.IsNullOrEmpty(X.TagNumber2)).
                    //    Select(X => X.RelEngineData2ID).
                    //    ToList();
                    CB.CableIDs = new List<string>();
                    CB.CableTagNumbers = new List<string>();

                    //所有在一端是开关的关联关系
                    //foreach (var item in RelBreaker_CableCol.
                    //    Where(x => x.RelEngineData1ID == CB.EngineerDataID && !string.IsNullOrEmpty(x.TagNumber2)))
                    //{
                    if (!string.IsNullOrEmpty(CB.CableID))
                    {
                        CB.CableIDs = CB.CableID.Split(',').ToList();//目前就考虑一个
                        CB.CableTagNumbers = engineBll.GetListSingleTable(ProjID, CB.CableIDs).Select(x => x.TagNumber).ToList();
                        //另一边的开关(cableid相同 但是开关id不同)
                        foreach (var RemoteItem in resAll.Where(x => x.CableID == CB.CableID && x.EngineerDataID != CB.EngineerDataID))
                        {
                            //        var remoteCB = ec_CircuitBreakerService.GetList("{ProjectId:\"" + ProjID + "\",EngineerDataID:\"" + RemoteItem.RelEngineData1ID + "\"}").FirstOrDefault();
                            //        if (remoteCB != null)
                            //        {
                            CB.RemoteCB = RemoteItem;
                            CB.RemoteDeviceId = RemoteItem.PDBId;
                            CB.RemoteDeviceName = RemoteItem.PDBName;
                            CB.RemoteDeviceObjectType = RemoteItem.ObjectTypeName;
                            //        }
                            //    }
                        }
                    }
                    else
                    {
                        CB.CableIDs = new List<string>();
                        CB.CableTagNumbers = new List<string>();
                    }
                    //    CB.CableTagNumbers.Add(item.TagNumber2);
               
                  
                }
                return res;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

        #region 提交数据
        public void DeleteEntity(string ProjectId, string BreakerID)
        {


            ec_CircuitBreakerService.Delete(ProjectId, BreakerID);

        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="BreakerID"></param>
        /// <param name="entity"></param>
        /// <param name="RelTypeId"></param>
        /// <param name="PBDId"></param>
        public void SaveEntity(string ProjectId, ref string BreakerID, ec_CircuitBreakerEntity entity, string PBDId)
        {
            try
            {
                if (entity != null)
                {
                    #region New  
                    entity.PDBId = PBDId;
                    ec_CircuitBreakerService.SaveEntity(ProjectId, ref BreakerID, entity);
                    #endregion
                }

                //}


            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

    }
}
